<script type="text/javascript" src="java/formval.js"></script>
<script type="text/javascript">
function validateOnSubmit(formElem) {
	var elem;
	var errs=0;
	if (!validatePresent(formElem.Erwachsene,  'ErwachseneError', true))  			errs = 1;
	if (!validatePresent(formElem.Datum,  'AnkunfsdatumError', true)) 	errs = 1;
	if (!validatePresent(formElem.Ubernacht,  'UbernachtError', true)) 	errs = 1;
	
	if (errs==0){
		formElem.submit();
	}
};


</script>
<?php

function GetListNummer($ListName,$ListNameError,$Count,$selectID=null){
	$KategorieList = "<select name='$ListName' id='$ListName' onchange='validatePresent(this, \"$ListNameError\");'>";
	if($selectID){
		$KategorieList.='<option value="'.$selectID.'" selected="selected">'.$selectID.'</option>';
		}else{
		$KategorieList.='<option value="">Please Select</option>';
	}
	for($i=1;$i<=$Count;$i++){
		$KategorieList.="<option value='$i'>$i</option>";
	}
	return $KategorieList;
}

	$ZimmerID = $_GET['ZimmerID'];
	$TouristID = $_GET["TouristID"];
	$BuchungID =  $_GET["BuchungID"];
	$AbteilungID = $_GET["AbteilungID"];
	$TagID =  $_GET["tag"];
	
	if($_POST){
		if(!empty($_POST['BuchungID'])){
			     date_default_timezone_set('UTC');
				list($d, $m, $y) = explode('/', $_POST["Datum"]);
				if(!$d || !$m || !$y){
					list($y ,$m, $d) = explode('-', $_POST["Datum"]);
				}
				$mk=mktime(0, 0, 0, $m, $d, $y);
				$date1=strftime('%Y-%m-%d',$mk);
				$sql="update buchung set 
				TouristID='$TouristID'
				,ZimmerID='$ZimmerID'
				,Erwachsene='".$_POST['Erwachsene']."'
				,Kinder='".$_POST["Kinder"]."'
				,Preis='".$_POST["Preis"]."'
				,Ankunfsdatum=now()
				,Ubernacht='".$_POST["Ubernacht"]."'" .
				",Datum='$date1'
				where ID=$BuchungID";
				$_rd=mysql_query($sql,$_db_link);
				$BuchungID = $_POST['BuchungID'];
				$url = "index.php?tag=$TagID&page=BuchungStep2&MoudeTagID=$MoudeTagID&ZimmerID=$ZimmerID" .
						"&BuchungID=$BuchungID&AbteilungID=$AbteilungID&TouristID=$TouristID";
				GotoURL($url);
		}else{
			if(!empty($_GET["ZimmerID"])&&!empty($_POST["Erwachsene"])&&!empty($_POST["Preis"])&&!empty($_POST["Datum"])
			&&!empty($_POST["Ubernacht"])){		//add		
			     $sql="insert into 
			     buchung (TouristID,ZimmerID,Erwachsene,Kinder,Preis,Ankunfsdatum,Ubernacht,Datum) 
				values(" .
				"'".$_POST["TouristID"]."'
				,".$_GET["ZimmerID"]."
				,'".$_POST["Erwachsene"]."'
				,'".$_POST["Kinder"]."'
				,'".$_POST["Preis"]."'
				,'".$_POST["Ankunfsdatum"]."'
				,'".$_POST["Ubernacht"]."'
				,'".$_POST["Datum"]."')";
				$_rd= mysql_query($sql,$_db_link); 
				$BuchungID = mysql_insert_id();
				$url = "index.php?tag=$TagID&page=BuchungStep2&MoudeTagID=$MoudeTagID&ZimmerID=$ZimmerID" .
						"&BuchungID=$BuchungID&AbteilungID=$AbteilungID&TouristID=$TouristID";
				GotoURL($url);
			}
		}
	}			
if($BuchungID){
	$sql = "select * from buchung where ID=$BuchungID";
	$_rd= mysql_query($sql,$_db_link); 
	if($_rd){
		$datenBuchung=mysql_fetch_array($_rd,MYSQL_ASSOC);
	}
}
	?>
	<div style="width:100%;background-color:#F1EEF3;margin:0px"> 
	<table class="tableEdit">
	<?PHP if(isset($BuchungID)){ ?>
		<tr>
		<td class="lableEdit"><?PHP  LangText("ID");?>:</td>
		<td class="inputEdit"><input type="text" name="BuchungID" id="BuchungID" READONLY value='<?PHP echo $BuchungID; ?>' /></td>
		<td class="validateEdit"></td>
		</tr>
		<?PHP } ?>
	<?PHP if(isset($TouristID)){ ?>	
	<tr>
	<td class="lableEdit"><?PHP  LangText("TouristID");?>:</td>
	<td class="inputEdit"><input type="text" name="AbteilungID" id="AbteilungID" READONLY value='<?PHP echo $datenBuchung["TouristID"]; ?>' /></td>
	<td class="validateEdit"></td>
	</tr>
	<?PHP } ?>
	<tr>
	<td class="lableEdit"><?PHP  LangText("Erwachsene");?>:*</td>
	<td class="inputEdit"><?PHP echo GetListNummer("Erwachsene","ErwachseneError",$daten["MaxErwachsenePersonen"],$datenBuchung['Erwachsene']); ?></td>
	<td class="validateEdit"><input type="label"  DISABLED  id="ErwachseneError" value='<?PHP LangText("ErwachseneError");?>' /></td>
	</tr>
	<tr>
	<td class="lableEdit"><?PHP  LangText("Kinder");?>:</td>
	<td class="inputEdit"><?PHP echo GetListNummer("Kinder","",$daten["MaxKinderPersonen"],$datenBuchung['Kinder']); ?></td>
	<td class="validateEdit"><input type="label"  DISABLED  id="KinderError" value='<?PHP LangText("KinderError");?>' /></td>
	</tr>
	<tr>
	<td class="lableEdit"><?PHP  LangText("Ubernacht");?>:*</td>
	<td class="inputEdit"><?PHP echo GetListNummer("Ubernacht","UbernachtError",30,$datenBuchung['Ubernacht']); ?></td>
	<td class="validateEdit"><input type="label"  DISABLED  id="UbernachtError" value='<?PHP LangText("UbernachtError");?>' /></td>
	</tr>
	<tr>
	<td class="lableEdit"><?PHP  LangText("Preis");?>:*</td>
	<td class="inputEdit"><input name="Preis" READONLY  onchange="validatePresent(this, 'PreisError');" id="Preis" value='<?PHP echo $datenBuchung["Preis"]; ?>' /></td>
	<td class="validateEdit"><input type="label"  DISABLED  id="PreisError" value='<?PHP LangText("PreisError");?>' /></td>
	</tr>
	<tr>
	<td class="lableEdit"><?PHP  LangText("Datum");?>:*</td>
	<td class="inputEdit"><input type="text" name="Datum" id="Datum" onchange="validatePresent(this, 'AnkunfsdatumError');" value='<?PHP echo $datenBuchung["Datum"]; ?>'>
&nbsp;<img src="phpimages/calendar.png" id="cal_Ankunfsdatum" name="cal_Ankunfsdatum" alt="" title="" style="cursor:pointer;cursor:hand;">
	<script type="text/javascript">
	Calendar.setup({
		inputField: "Datum", // input field id
		ifFormat: "%d/%m/%Y", // date format
		button: "cal_Ankunfsdatum" // button id
	});
	</script>
</td>
	<td class="validateEdit"><input type="label"  DISABLED  id="AnkunfsdatumError" value='<?PHP LangText("AnkunfsdatumError");?>' /></td>
	</tr>
	<tr>
	<td class="lableEdit"></td>
	<td class="inputEdit">
	<input type="button"  onclick="window.location='index.php?tag=<?PHP echo $_GET['tag']; ?>&page=FirmaView&AbteilungID=<?PHP echo $_GET['AbteilungID']; ?>&MoudeTagID=<?PHP echo $_GET['MoudeTagID']; ?>'" class="BootenLink" value='<?PHP LangText("Abbrechen");?>' />
	<input type="button"  onclick="validateOnSubmit(this.form);" class="BootenLink" value='<?PHP LangText("Weiter");?>' />
	</td>
	<td class="validateEdit"></td>
	</tr>
	</table>
	</div>
	
	<?php
?> 
<script type="text/javascript">
function timedCount()
{
	c = 0;
	c = document.getElementById('Erwachsene').value*document.getElementById('PreisErwachsene').value;
	if(document.getElementById('Kinder').value!='')
		c = c + document.getElementById('Kinder').value*document.getElementById('KinderPreis').value;
	c = c * document.getElementById('Ubernacht').value;
	//alert(c);
	document.getElementById('Preis').value = c;
	t=setTimeout("timedCount()",100);
}
timedCount();
</script>

